﻿using Microsoft.AspNetCore.Mvc;
using BJ.AuthEntity.Models;
using BJ.AuthEntity.ViewModels;
using Syspetro.Core.AppDeal;
using Syspetro.Core.DynamicApi;
using Syspetro.Deploy.AuthorizationReq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using BJ.AuthDomain.IService;
using Microsoft.AspNetCore.Authorization;

namespace BJ.AuthApp.Admin
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserApp : BaseDynamicApi
    {
        readonly IUserService UserService;
        readonly IUserAuthService UserAuthService;

        public UserApp(IUserService userService, IUserAuthService userAuthService)
        {
            UserService = userService;
            UserAuthService = userAuthService;
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="vuser"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        public async Task<SpActionResult<Guid>> AddUser(VM_User_Add vuser)
        {
            if (string.IsNullOrEmpty(vuser.Name) || string.IsNullOrEmpty(vuser.AuthName))
                return new SpActionResult<Guid> {  Success = false, Msg = "名称不可为空" };
            return new SpActionResult<Guid>(await UserService.AddUserAsync(vuser));
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="vuser"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        public async Task<SpActionResult<int>> AddUsers(List<VM_User_Add> vuser)
        {
            return Result(await UserService.AddUsersAsync(vuser));
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        public async Task<SpActionResult<bool>> DeleteUser(Guid id)
        {
            return Result(await UserService.DeleteAsync(id));
        }
        /// <summary>
        /// 冻结用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        [HttpPut]
        public async Task<SpActionResult<bool>> FreeezeUser(Guid id)
        {
            return await UserAuthService.FreezeUser(id);
        }
        /// <summary>
        /// 恢复用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        [HttpPut]
        public async Task<SpActionResult<bool>> ResumeUser(Guid id)
        {
            return await UserAuthService.ResumeUser(id);
        }
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="vuser"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        public async Task<SpActionResult<bool>> UpdateUser(VM_User_Update vuser)
        {
            return Result(await UserService.UpdateAsync<VM_User_Update>(vuser, t => t.Id == vuser.Id));
        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        public async Task<SpActionResult<VM_User>> GetUser(Guid id)
        {
            //var list = await UserService.GetAsync(id);
            //return list.Adapt<VM_User>();
            return Result(await UserService.GetUserFull(id));
        }
        /// <summary>
        /// 验证账号是否存在
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        public async Task<SpActionResult<bool>> Verify(string account)
        {
            return Result(await UserService.Verify(account));
        }
        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        public async Task<SpActionResult<VM_User>> UserSearch(VM_User_Search search)
        {
            return Result(await UserService.UserSearch(search));
        }
        /// <summary>
        /// 分页查找所有角色
        /// </summary>
        /// <param name="pageReq"></param>
        /// <returns></returns>
        [Authorize]
        [Admin]
        [ProducesResponseType(typeof(VM_User), 200)]
        public async Task<SpActionResult<PageList<VM_User>>> FindRoles([FromBody] VM_User_PageReq pageReq)
        {
            if (pageReq == null)
                pageReq = new VM_User_PageReq();
            //return await UserService.FindPageAsync<VM_User>(pageReq);
            return Result(await UserService.FindUsersFull(pageReq));
        }
        /// <summary>
        /// 获取用户注册总数
        /// </summary>
        /// <returns></returns>
        [ProducesResponseType(typeof(long), 200)]
        public async Task<long> GetUserCount()
        {
            
            return await UserService.GetUserCount();
        }
        /// <summary>
        /// 分页查找所有账户
        /// </summary>
        /// <returns></returns>
        [ProducesResponseType(typeof(long), 200)]
        [Authorize]
        [Admin]

        public async Task<SpActionResult<PageList<VM_UserMis>>> GetUserList(VM_User_Page pageReq)
        {

            return Result(await UserService.GetUserList(pageReq));
        }
    }
}
